const mongoose = require('mongoose');

const carSchema = new mongoose.Schema({
    // 基础信息
    brand: {
        type: String,
        required: [true, '品牌不能为空']
    },
    model: {
        type: String,
        required: [true, '型号不能为空']
    },
    level: {
        type: String,
        enum: ['经济型', '舒适型', '豪华型', '商务型'],
        required: [true, '车辆级别不能为空']
    },
    type: {
        type: String,
        enum: ['轿车', 'SUV', 'MPV', '跑车', '面包车'],
        required: [true, '车辆类型不能为空']
    },

    // 租赁信息
    dailyRate: {
        type: Number,
        required: [true, '日租金不能为空'],
        min: [0, '日租金不能小于0']
    },
    status: {
        type: String,
        enum: ['可租', '已租', '维修中'],
        default: '可租'
    },

    // 位置信息
    city: {
        type: String,
        required: [true, '取车城市不能为空']
    },
    returnCities: [{  // 改为数组形式，支持多个还车城市
        type: String,
        required: [true, '还车城市不能为空']
    }],
    location: {
        type: String,
        required: [true, '具体地址不能为空']
    },

    // 图片信息
    coverImage: {
        type: String,
        required: [true, '封面图片不能为空']
    },
    images: [{
        type: String
    }],

    // 详细参数
    specifications: {
        length: Number,  // 长度(mm)
        width: Number,   // 宽度(mm)
        height: Number,  // 高度(mm)
        seats: {
            type: Number,
            required: [true, '座位数不能为空']
        },
        trunkCapacity: Number,  // 后备箱容积(L)
        powerType: {
            type: String,
            enum: ['燃油', '纯电动', '混动'],
            required: [true, '动力类型不能为空']
        },
        range: Number,          // 续航里程(km)
        fuelConsumption: Number // 油耗(L/100km)
    },

    // 租赁历史
    rentalHistory: {
        rentCount: {
            type: Number,
            default: 0
        },
        averageRating: {
            type: Number,
            default: 0,
            min: 0,
            max: 5
        }
    },

    // 其他信息
    description: String,
    additionalServices: [{
        name: String,
        price: Number,
        description: String
    }],

    // 创建和更新时间
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 更新时自动更新 updatedAt 字段
carSchema.pre('save', function (next) {
    this.updatedAt = new Date();
    next();
});

// 修改导出为mongoose模型
module.exports = mongoose.model('Car', carSchema); 